home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5435 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.6 KB  |  205 lines

  1. Path: newsfeed.internetmci.com!xmission!news
  2. From: tknarr@xmission.com  ( Todd Knarr )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Date class
  5. Date: 4 Feb 1996 18:05:00 GMT
  6. Organization: Chaos Central
  7. Message-ID: <4f2sgc$bct@news.xmission.com>
  8. References: <4f09k2$nnu@george.rutgers.edu>
  9. Reply-To: tknarr@xmission.com ( Todd Knarr )
  10. NNTP-Posting-Host: slc68.xmission.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. Here's the .C file that completes the Date class.
  14.  
  15. //
  16. //  Classname : Date
  17. //
  18. //  Author    : Todd Knarr
  19. //
  20. //  Description    :
  21. //      Provides a Date class which represents dates as Julian day numbers
  22. //      ( days since 1 Jan 4713 BC ).
  23. //      This class can handle all dates from  1 Jan 4713BC to 31 Dec 9999AD.
  24. //
  25. //      Note: Years AD are positive, years BC are negative. There is
  26. //      no year 0AD, it goes from 1BC to 1AD. A year of 0 will be treated
  27. //      as 1BC. No attempt is made to validate ranges. Physical errors
  28. //      will not result from insane day-month combinations like the 87th
  29. //      day of the 45th month, but the results will obviously not make
  30. //      much sense.
  31. //
  32. //      Date conversion routines by Eric Bergman-Terrell,
  33. //          Computer Language Dec 1990.
  34. //
  35.  
  36. #include "date.h"
  37.  
  38. #include <time.h>
  39.  
  40. //
  41. //  Function :  YmdToJd
  42. //
  43. //  Parameters     : int year, month, day
  44. //
  45. //  Return values  : long julian day
  46. //
  47. //  Description    :
  48. //
  49. long Date::YmdToJd( const int iYear, const int iMonth, const int iDay )
  50. {
  51.     long jul_day;
  52.  
  53.     int a,b;
  54.     int year = iYear, month = iMonth, day = iDay;
  55.     float year_corr;
  56.  
  57.     if ( year < 0 )
  58.         year++;
  59.     year_corr = ( year > 0 ? 0.0 : 0.75 );
  60.     if ( month <= 2 )
  61.     {
  62.         year--;
  63.         month += 12;
  64.     }
  65.     b = 0;
  66.     if ( year * 10000.0 + month * 100.0 + day >= 15821015.0 )
  67.     {
  68.         a = year / 100;
  69.         b = 2 - a + a / 4;
  70.     }
  71.     jul_day = (long) ( 365.25 * year - year_corr ) +
  72.               (long) ( 30.6001 * ( month + 1 ) ) + day + 1720995L + b;
  73.  
  74.     return jul_day;
  75. }
  76.  
  77. //
  78. //  Function :  JdToYmd
  79. //
  80. //  Parameters     : long julian day, pointers to int year, month, day
  81. //
  82. //  Return values  : none
  83. //
  84. //  Description    :
  85. //
  86. void Date::JdToYmd( const long lJD, int *piYear, int *piMonth, int *piDay )
  87. {
  88.     long a, b, c, d, e, z, alpha;
  89.  
  90.     z = lJD;
  91.     if ( z < 2299161L )
  92.         a = z;
  93.     else
  94.     {
  95.         alpha = (long) ( ( z - 1867216.25 ) / 36524.25 );
  96.         a = z + 1 + alpha - alpha / 4;
  97.     }
  98.     b = a + 1524;
  99.     c = (long) ( ( b - 122.1 ) / 365.25 );
  100.     d = (long) ( 365.25 * c );
  101.     e = (long) ( ( b - d ) / 30.6001 );
  102.     *piDay = (int) b - d - (long) ( 30.6001 * e );
  103.     *piMonth = (int) ( e < 13.5 ) ? e - 1 : e - 13;
  104.     *piYear = (int) ( *piMonth > 2.5 ) ? ( c - 4716 ) : c - 4715;
  105.     if ( *piYear <= 0 )
  106.         *piYear -= 1;
  107.  
  108.     return;
  109. }
  110.  
  111. //
  112. //  Function :  ToString
  113. //
  114. //  Parameters     : pointer to string buffer to fill in
  115. //
  116. //  Return values  : none
  117. //
  118. //  Description    : Formats the Date into an ASCII representation.
  119. //              This is the ASCII form of the long Julian day number.
  120. //              The string is a fixed-length 11-character string, including
  121. //              the NUL terminator;
  122. //
  123. void Date::ToString( char *szBuffer ) const
  124. {
  125.     int i;
  126.     long Temp;
  127.  
  128.     Temp = lJulianDay;
  129.     if ( Temp < 0L )
  130.         szBuffer[0] = '-';
  131.     else
  132.         szBuffer[0] = '+';
  133.     szBuffer[10] = '\0';
  134.     for ( i = 9; i > 0; i-- )
  135.     {
  136.         szBuffer[i] = ( Temp % 10L ) + '0';
  137.         Temp /= 10;
  138.     }
  139.  
  140.     return;
  141. }
  142.  
  143. //
  144. //  Function :      DayOfYear
  145. //
  146. //  Parameters     : none
  147. //
  148. //  Return values  : day within year
  149. //
  150. //  Description    :
  151. //
  152. int Date::DayOfYear( void ) const
  153. {
  154.     int y, m, d;
  155.     long soy;
  156.  
  157.     JdToYmd( lJulianDay, &y, &m, &d );
  158.     soy = YmdToJd( y, 1, 1 );
  159.     return (int) ( lJulianDay - soy + 1 );
  160. }
  161.  
  162. //
  163. //  Function :      ToSysTime
  164. //
  165. //  Parameters     : none
  166. //
  167. //  Return values  : converted result
  168. //
  169. //  Description    : Converts the date to a time_t value
  170. //          representing midnight of that date.
  171. //
  172. time_t Date::ToSysTime( void ) const
  173. {
  174.     struct tm tmRep;
  175.     int y, m, d;
  176.     time_t t;
  177.     
  178.     JdToYmd( lJulianDay, &y, &m, &d );
  179.     if ( y < 1970 )
  180.     {
  181.         y = 1970;
  182.         m = 1;
  183.         d = 1;
  184.     }
  185.     tmRep.tm_year = y - 1900 ;
  186.     tmRep.tm_mon = m;
  187.     tmRep.tm_mday = d;
  188.     tmRep.tm_hour = 0;
  189.     tmRep.tm_min = 0;
  190.     tmRep.tm_sec = 0;
  191.     tmRep.tm_isdst = 0;
  192.     
  193.     t = mktime( &tmRep );
  194.     return t;
  195. }
  196.  
  197. --
  198. Todd Knarr : tknarr@xmission.com      |  finger for PGP public key
  199.                                       |  Member, USENET Cabal
  200.  
  201. Seriously, I don't want to die just yet.  I don't care how
  202. good-looking they are, I! don't! want! to! die!"
  203.                                         -- Megazone ( UF1 )
  204.  
  205.